﻿$stopPropagation = function (e) {
    if (e && e.stopPropagation)
        e.stopPropagation();
    else
        window.event.cancelBubble = true;
}
var Tool = {
    NodeAttributesToObj: function (node) {
        var obj = new Object();
        if (node && node.attributes && node.attributes.length != 0) {
            for (var i = 0; i < node.attributes.length; i++) {
                obj[node.attributes[i].nodeName] = node.attributes[i].nodeValue;
            }
        }
        return obj;
    },
    Animate: {
        Text_ShadowAnimate: function (canvas, speed, callback, text, font, color, x, y) {
            speed = speed || config.textShadowAnimateTimeSpeed;
            if (canvas) {
                font_size = 20;
                speed = speed || 100;
                text = text || "!";
                font = font || "bold " + font_size.toString() + "px 宋体,Arial, Helvetica, sans-serif";
                color = "rgba(" + (color || "0,0,0") + ",1)";
                x = x || 0;
                y = y || 0;
                var context = canvas.getContext("2d");
                var shadowBlur = 1;
                var span = 0.5;
                var interval = window.setInterval(function () {
                    context.save();
                    context.shadowColor = color;
                    context.textBaseline = "top";
                    context.font = font;
                    context.fillStyle = color;
                    shadowBlur += span;
                    context.shadowBlur = shadowBlur;
                    context.clearRect(x, y, context.measureText(text).width + 5, font_size + 5);
                    context.fillText(text, x, y);
                    if (context.shadowBlur >= 5)
                        span = 0 - span;
                    else if (context.shadowBlur <= 1)
                        span = span - span * 2;
                    context.restore();
                }, speed);
                return {
                    stop: function () {
                        window.clearInterval(interval);
                    }
                };
            }
        },
        Box_Hide_Animate: function (img, canvas, callback, x, y, w, h, speed, span) {
            if (img && canvas) {
                x = x != null ? x : 0;
                y = y != null ? y : 0;
                w = w != null ? w : canvas.width;
                h = h != null ? h : canvas.height;
                speed = speed || config.boxAnimateTimeSpeed;
                span = span || 10;
                var spanY = span / (w / h);
                var context = canvas.getContext("2d");
                context.save();
                Tool.ResetContext(context);
                var box_Interval = window.setInterval(function () {
                    context.clearRect(x - 1, y - 1, w + 1, h + 3);
                    x = x + span;
                    y = y + spanY;
                    w = w - span * 2;
                    h = h - spanY * 2;
                    if (w >= 0 || h >= 0) {
                        context.drawImage(img, 0, 0, img.width, img.height, x, y, w, h);
                    }
                    else {
                        window.clearInterval(box_Interval);
                        if (callback) {
                            context.restore();
                            callback();
                        }
                    }
                }, speed);
            }
        },
        Box_Show_Animate: function (img, canvas, callback, x, y, w, h, speed, span) {
            if (img && canvas && x && y) {
                w = w != null ? w : canvas.width;
                h = h != null ? h : canvas.height;
                speed = speed || config.boxAnimateTimeSpeed;
                span = span || 10;
                var dx = x - w / 2;
                var dy = y - h / 2;
                var dw = 0;
                var dh = 0;
                var spanY = span / (w / h);
                var context = canvas.getContext("2d");
                context.save();
                Tool.ResetContext(context);
                var box_Interval = window.setInterval(function () {
                    context.clearRect(x - 1, y - 1, w + 1, h + 3);
                    x = x - span;
                    y = y - spanY;
                    dw = dw + span * 2;
                    dh = dh + spanY * 2;
                    if (dw <= w || dh <= h) {
                        context.drawImage(img, 0, 0, img.width, img.height, x, y, dw, dh);
                    }
                    else {
                        window.clearInterval(box_Interval);
                        context.drawImage(img, 0, 0, img.width, img.height, dx, dy, img.width, img.height);
                        if (callback) {
                            context.restore();
                            callback();
                        }
                    }
                }, speed);
            }
        },
        ImageAnimate: function (img, canvas, x, y, w, h, mode, speed, redraw) {
            speed = speed || 50;
            var i = 0;
            var dx = 0;
            var dy = 0;
            var _this = this;
            var context = canvas.getContext("2d");
            var interval = window.setInterval(function () {
                context.save();
                Tool.ResetContext(context);
                context.clearRect(x, y, w, h);
                if (redraw) redraw(x, y, w, h);
                context.drawImage(img, dx, dy, w, h, x, y, w, h);
                if (mode == "x") {
                    dx += w;
                    if (dx == img.width)
                        dx = 0;
                }
                else if (mode == "y") {
                    dy += h;
                    if (dy == img.height)
                        dy = 0;
                }
                context.restore();
            }, speed);
            return {
                stop: function () {
                    window.clearInterval(interval);
                    context.clearRect(x, y, w, h);
                }
            };
        }
    },
    ResetContext: function (context, font, fillStyle) {
        context.shadowColor = 'rgba(0, 0, 0, 0)';
        context.shadowOffsetX = 0;
        context.shadowOffsetY = 0;
        context.shadowBlur = 0;
        context.font = font || "";
        context.fillStyle = fillStyle || "";
        context.textBaseline = "top";
    },
    Image: {
        Load: function (src, onload, onerror) {
            var image = new Image();
            image.src = src;
            if (onload)
                image.onload = onload;
            if (onerror)
                image.onerror = onerror;
            return image;
        }
    },
    Menu: {
        Draw: function (img, context, menu, array, T, spanX, spanY) {
            var x = menu.x;
            var y = menu.y;
            var w = menu.w;
            var h = menu.h;
            for (var i = 0; i < menu.objArray.length; i++) {
                var m = menu.objArray[i];
                var eventobj = new T(m, context, img, x, y, w, h);
                eventobj.mouseout();
                array.push(eventobj);
                x += spanX;
                y += h + spanY;
            }
        }
    },
    Mouse: {
        GetX: function (e, obj) {
            var x;
            if (e.pageX != undefined && e.pageY != undefined) {
                x = e.pageX;
            }
            else {
                x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            }
            var left = obj.offsetLeft;
            while (obj = obj.offsetParent) {
                left += obj.offsetLeft;
            }
            return x - left;
        },
        GetY: function (e, obj) {
            var y;
            if (e.pageX != undefined && e.pageY != undefined) {
                y = e.pageY;
            }
            else {
                y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            }
            var top = obj.offsetTop;
            while (obj = obj.offsetParent) {
                top += obj.offsetTop;
            }
            return y - top;
        }
    },
    drawProgress: function (canvas, x, y, w, h, max, now, boxImage) {
        var context = canvas.getContext("2d");
        context.save();
        Tool.ResetContext(context);
        var step = now / max * w;
        var gradient = context.createLinearGradient(x, 0, step, 0);
        gradient.addColorStop(0, "rgba(255,255,255,1)");
        gradient.addColorStop(1, "blue");
        context.fillStyle = gradient;
        context.clearRect(x, y, w, h);
        context.fillRect(x, y, step, h);
        context.restore();
    },
    audioCreate: function (src, canplaythrough, error, ended, loop) {
        var a = new Audio();
        a.src = src;
        if (canplaythrough)
            a.addEventListener("canplaythrough", canplaythrough, false);
        if (error)
            a.addEventListener("error", error, false);

        if (loop) {
            if ($.browser.mozilla) {
                a.addEventListener('ended', ended, false);
            }
            else {
                a.loop = true;
            }
        }
        else if (ended) {
            a.addEventListener('ended', ended, false);
        }
        return a;
    }
}